
Ext.onReady(function(){
    var current_order_id; //当前选择的order的id
    var current_ticket_id; //当前选择的ticket的id
    var current_order_status; //当前order的状态
    var current_ticket_status; //当前ticket的状态
    var ordersrow;
	var ticketsrow;
	
	var before = new Ext.form.Checkbox({
        fieldLabel: '以前的',
        hideLabel: true,
        boxLabel: '以前的',
        name: 'before',
        checked: false,
        anchor: '100%'
    });
	var myOrder = new Ext.form.Checkbox({
        fieldLabel: '我的订单',
        hideLabel: true,
        boxLabel: '我的订单',
        name: 'my_order',
        checked: true,
        anchor: '100%'
    });
	var date_field = new Ext.form.DateField({
        fieldLabel: '确认日期',
        name: 'affirm_date',
        altFormats: 'Y-m-d',
        format: 'Y-m-d', //格式化日期
        //disabledDays: [0,6],	//不让选择周六，周日
        //disabledDaysText: "周末要休息",
        invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
        allowBlank: false,
        //emptyText: '请选择...',
        inputType: '2008-01-01',
        //anchor: '50%'
        width: 90,
        value: today
    });
    //提交按钮
    var submitButton = new Ext.Button({
        text: '查询',
        handler: function(){
            if (searchFormPanel.form.isValid()) {
                var date = date_field.getValue().format('Y-m-d');
                var isBefore = "";
                if (before.checked) {
                    isBefore = "yes";
                }
				var isMy = "";
                if (myOrder.checked) {
                    isMy = "yes";
                }
				
                wqr_orderDS.baseParams.before = isBefore;
                wqr_orderDS.baseParams.date = date;
                wqr_orderDS.baseParams.isMy = isMy;
                wqr_orderDS.reload();
                
                wqr_ticketDS.baseParams.id = "";
                wqr_ticketDS.reload();
                
            }
        }
    })
    
    var searchFormPanel = new Ext.form.FormPanel({
        //baseCls: 'x-plain',
        labelWidth: 75,
        width: '100%',
        frame: true,
        labelAlign: 'right',
        items: [{
            layout: 'column',
            border: false,
            items: [{
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [date_field]
            }, {
                columnWidth: .15,
                layout: 'form',
                border: false,
                items: [before]
            }, {
                columnWidth: .17,
                layout: 'form',
                border: false,
                items: [myOrder]
			}, {
                columnWidth: .3,
                layout: 'form',
                border: false,
                items: [submitButton]
            }]
        }]
    });
    
    searchFormPanel.render('wqr_search_form');
	
    //############## 订单Grid ##############
    var wqr_orderCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
		hidden: true
    }, {
        header: '订单编号',
        dataIndex: 'order_no',
        width: 100,
		hidden: true
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 50
    }, {
        header: '客户名称',
        dataIndex: 'customer',
        width: 70
    }, {
        header: '订单状态',
        dataIndex: 'status',
        width: 70
    }, {
        header: '总金额',
        dataIndex: 'price_sum',
        width: 70
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '确认日期',
        dataIndex: 'affirm_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 70
    }, {
        header: '送票地址',
        dataIndex: 'send_address',
        width: 200
    }, {
        header: '送票时间',
        dataIndex: 'send_time',
        width: 70
    }, {
        header: '联系人',
        dataIndex: 'link_man',
        width: 70,
		hidden: true
    }, {
        header: '联系电话',
        dataIndex: 'link_tel',
        width: 100,
		hidden: true
    }, {
        header: '预定人',
        dataIndex: 'creator',
        width: 70
    }, {
        header: '预定时间',
        dataIndex: 'created_at',
        width: 120,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
		hidden: true
	}, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        width: 50
    }, {
        header: '客户要求',
        dataIndex: 'order_request',
        width: 120
    }]);
    
    var wqr_orderDS = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/orders/getWQR'
        }),
        reader: orderReader
    });
    
    var wqr_orderGridPanel = new Ext.grid.GridPanel({
        el: 'wqr_ordersGrid',
        ds: wqr_orderDS,
        cm: wqr_orderCM,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 310,
        title: '订单信息'
    });
    
    wqr_orderGridPanel.render();
    wqr_orderDS.load();
    
    //双击订单查看机票详细
    wqr_orderGridPanel.on('rowdblclick', function(wqr_orderGridPanel, rowIndex, e){
        var current_order_selected = wqr_orderDS.data.items[rowIndex].id;
        ordersrow = wqr_orderDS.getById(current_order_selected);//ds为绑定到grid的数据源 
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        wqr_ticketDS.baseParams.id = current_order_id;
        wqr_ticketDS.reload();
    });
    //订单右键菜单
    wqr_orderGridPanel.on('rowcontextmenu', function(customergridpanel, rowIndex, e){
        var current_order_selected = wqr_orderDS.data.items[rowIndex].id;
        ordersrow = wqr_orderDS.getById(current_order_selected);//ds为绑定到grid的数据源 
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        e.preventDefault();
        rightClick.showAt(e.getXY());
    });
    
    //############ 订单-右键菜单 ##########
    var rightClick = new Ext.menu.Menu({
        items: [{
            //id: ‘rightMenu1′, 
            handler: rightMenu1Fn,
            text: '【修改订单】'
        }, {
            //id: ‘rightMenu2′, 
            handler: rightMenu2Fn,
            text: '【取消订单】'
        }]
    });
    function rightMenu1Fn(){
        ModOrder(wqr_orderDS, current_order_id, wqr_ticketDS);
    }
    function rightMenu2Fn(){
        var current_order_no = ordersrow.get("order_no");
        cancelOrder(current_order_status, current_order_id, current_order_no, wqr_orderDS, wqr_ticketDS);
    }

    //### 机票Grid ###
    var wqr_ticketCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id'
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 50
    }, {
        header: '旅客姓名',
        dataIndex: 'passenger_name',
        width: 70
    }, {
        header: '证件号码',
        dataIndex: 'card',
        width: 140
    }, {
        header: '票号',
        dataIndex: 'ticket_no',
        width: 100
    }, {
        header: '状态',
        dataIndex: 'status_type',
        width: 70
	}, {
        header: '出票状态',
        dataIndex: 'status',
        sortable: true,
        width: 70
    }, {
        header: '航班号',
        dataIndex: 'air_no',
        width: 70
    }, {
        header: '航程',
        dataIndex: 'fromto',
        width: 70
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 70
    }, {
        header: '舱位',
        dataIndex: 'position',
        width: 50
    }, {
        header: '机票价格',
        dataIndex: 'price',
        width: 70
    }, {
        header: '机场费税',
        dataIndex: 'airdrome_tax',
        width: 70
    }, {
        header: '附加费',
        dataIndex: 'fuel_tax',
        width: 70
    }, {
        header: 'F',
        dataIndex: 'price_F',
        width: 50
    }, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        width: 70
    }, {
        header: '保险F',
        dataIndex: 'insurance_F',
        width: 50
    }, {
        header: '其他',
        dataIndex: 'other',
        width: 50
    }, {
        header: '积分',
        dataIndex: 'point',
        width: 50
    }, {
        header: '合计',
        dataIndex: 'total_price',
        width: 70
    
    }]);
    
    //隐藏ticket数据库id
    wqr_ticketCM.setHidden(1, !wqr_ticketCM.isHidden(1));
    var ticketProxy = new Ext.data.HttpProxy({
        url: '/orders/getTicketsByOrderID?showCancelTicket=no'
    });
    
    var wqr_ticketDS = new Ext.data.Store({
        proxy: ticketProxy,
        reader: ticketReader
    });
    
    var wqr_ticketGridPanel = new Ext.grid.GridPanel({
        el: 'wqr_ticketsGrid',
        ds: wqr_ticketDS,
        cm: wqr_ticketCM,
        //sm: sm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 160,
        title: '航班信息'
    });
    
    wqr_ticketGridPanel.render();
    wqr_ticketDS.load();
    
    //############ 票-右键菜单 ##########
    function ticketRightMenu1(){
        ModTicket(wqr_ticketDS, current_ticket_id, wqr_orderDS);
    }
    function ticketRightMenu2(){
        var current_passenger_name = ticketsrow.get("passenger_name");
		cancelTicket(current_ticket_status, current_ticket_id, current_passenger_name, wqr_ticketDS);
    }
    var ticketRightClick = new Ext.menu.Menu({
        items: [{
            id: 'show_ticket',
            handler: ticketRightMenu1,
            text: '【编辑修改】'
        }, {
            id: 'del_ticket',
            handler: ticketRightMenu2,
            text: '【取消乘客】'
        }]
    });
    wqr_ticketGridPanel.on('rowcontextmenu', function(wqr_ticketGridPanel, rowIndex, e){
        var current_ticket_selected = wqr_ticketDS.data.items[rowIndex].id;
        ticketsrow = wqr_ticketDS.getById(current_ticket_selected);//ds为绑定到grid的数据源 
        current_ticket_id = ticketsrow.get("id");
        current_ticket_status = ticketsrow.get("status");
        e.preventDefault();
        ticketRightClick.showAt(e.getXY());
    });
})
